package com.panda.framework.netty.handler.extend;


import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.EnumErrorCode;
import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.TransportMessage;
import com.alibaba.fastjson.JSON;
import com.panda.common.core.domain.AjaxResult;
import com.panda.common.core.domain.entity.SysUser;
import com.panda.framework.netty.common.Constant;
import com.panda.framework.netty.utils.MessageUtil;
import com.panda.system.service.ISysNoticeService;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CloseNoticeListWebsocketHandler {
    Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private ISysNoticeService service;
    /**
     * 消息已读
     * @author wechatno:tangjinjinwx
     * @param ctx
     * @param vo
     */
    @Async
    public  void handleMsg(ChannelHandlerContext ctx , TransportMessage vo, String contentJsonStr) {
        try {
            log.debug(contentJsonStr);
            List<Long> ids = JSON.parseArray(contentJsonStr, Long.class);
            if(null != ids){
                SysUser user = new SysUser();
                if(null != user){
                    sendJson(ctx, service,ids);
                }else{
                    MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_PARAMERROR);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
        }
    }

    private static void  sendJson(ChannelHandlerContext ctx, ISysNoticeService service,List<Long> ids){
        service.closeNotice(ids);
        String json  = JSON.toJSONString(AjaxResult.success()); ;
        //3、告诉PC客户端消息已收到
        MessageUtil.sendCustomJsonMsg(ctx, "CloseNoticeListResp", json);
    }


}
